package com.wuye.common.cmd;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class PrintBean2Sql {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println(genCreateTableSql("com.wuye.entity.Fangchan"));
		System.out.println(genCreateTableSql("com.wuye.entity.Jiaofei"));
		System.out.println(genCreateTableSql("com.wuye.entity.Tousu"));
		System.out.println(genCreateTableSql("com.wuye.entity.Users"));
		System.out.println(genCreateTableSql("com.wuye.entity.Weixiu"));
		
	}
	
	public static String getBeanName(String bean){
		try {
			Class clz = Class.forName(bean);
			String clzStr = clz.toString();
			//得到类名
			String beanName = clzStr.substring(clzStr.lastIndexOf(".")+1).toLowerCase();
			return beanName;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return "";
		}
	}
	
	public static List<String> getBeanPropertyList(String bean){
		try {
			Class clz = Class.forName(bean);
			Field[] strs = clz.getDeclaredFields();
			List<String> propertyList = new ArrayList<String>();
			for (int i = 0; i < strs.length; i++) {
				String protype = strs[i].getType().toString();
				propertyList.add(protype.substring(protype.lastIndexOf(".")+1)+"`"+strs[i].getName());
			}
			return propertyList;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getBeanFilesList(String bean){
		try {
			Class clz = Class.forName(bean);
			Field[] strs = clz.getDeclaredFields();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < strs.length; i++) {
				String protype = strs[i].getType().toString();
				if (!strs[i].getName().equals("tableName")&&!strs[i].getType().equals("List")) {
//				   sb.append("c_"+strs[i].getName()+",");
					sb.append(strs[i].getName()+",");
				}
			}
			sb.deleteCharAt(sb.toString().lastIndexOf(","));
			return sb.toString();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * @param bean
	 * @return
	 */
	public static String genCreateTableSql(String bean){
		List<String> beanPropertyList =  getBeanPropertyList(bean);
		StringBuffer sb = new StringBuffer("create table t_"+getBeanName(bean)+"(\n");
		for (String string : beanPropertyList) {
			String[] propertys = string.split("`");
			if (!propertys[1].equals("tableName")&&!propertys[1].equals("param")&&!propertys[0].equals("List")) {
				if (propertys[1].equals("id")) {
					sb.append("   id NUMBER(5) PRIMARY KEY,\n");
					//sb.append("   id bigint primary key auto_increment,\n");
				} else {
					if (propertys[1].contains("desc")) {
						sb.append("   " + propertys[1] + " varchar(256) default '',\n");
					} else 
					if (propertys[0].equals("int")) {
						sb.append("   " + propertys[1] + " int default 0,\n");
					} else if (propertys[0].equals("String")) {
						sb.append("   " + propertys[1] + " varchar(128) default '',\n");
					} else if (propertys[0].equals("double")) {
						sb.append("   " + propertys[1] + " double default 0.0,\n");
					} else if (propertys[0].equals("Date")) {
						sb.append("   " + propertys[1] + " DATE DEFAULT (sysdate),\n");
						//sb.append("   " + propertys[1] + " datetime,\n");
					} else if (propertys[0].equals("boolean")) {
						sb.append("   " + propertys[1] + " int(1) default 0,\n");
					} 
				}
			}
		}
		sb.deleteCharAt(sb.lastIndexOf(","));
		sb.append(");");
		return sb.toString();
	}
	
	/**
	 * 生成查询语句
	 * @param bean
	 * @return
	 */
	public static String genSelectAllSql(String bean){
		String filesList =  getBeanFilesList(bean);
		return "select \n "+filesList+" \n from \n t_"+getBeanName(bean)+"";
	}
	
	/**
	 * 生成插入语句
	 * @param bean
	 * @return
	 */
	public static String genInsertSql(String bean){
		String filesList =  getBeanFilesList(bean);
		int fl = getCountSonStr(filesList,",")+1;
		String wenhao = "";
		for (int i = 0; i < fl; i++) {
			if(i==fl-1){
				wenhao = wenhao+"?";
			}else{
				wenhao = wenhao+"?,";
			}
		}
		
		return "insert into t_"+getBeanName(bean)+"("+filesList+") values("+wenhao+")";
	}

	private static int getCountSonStr(String filesList, String str) {
		String[] arr = filesList.split(str);
		if(arr == null)
			return 0;
		return arr.length;
	}
}
